package net.kem.android.positioner;

import net.kem.android.positioner.R;
import android.app.PendingIntent;
import android.location.Location;

class POI {
	private String name;
//	private GeoPoint coordinates;
	private Location coordinates;
	private int resourceDrawable;
	private PendingIntent proximityIntent;

	POI(String name) {
		init(name, 0, 0);
	}
	POI(String name, Location coordinates) {
		init(name, coordinates.getLatitude(), coordinates.getLongitude());
	}

	POI(String name, String coords) {
		String[] latLon = coords.split(";");
		float latitude = Float.parseFloat(latLon[0]);
		float longitude = Float.parseFloat(latLon[1]);
		init(name, latitude, longitude);
	}

	POI(String name, double latitude, double longitude) {
		init(name, latitude, longitude);
	}

	private void init(String name, double latitude, double longitude) {
		this.name = name;
		this.coordinates = new Location("POI_" + name);
		this.coordinates.setLatitude(latitude);
		this.coordinates.setLongitude(longitude);
		this.resourceDrawable = R.drawable.greenflag;
	}

	static POI load(String poiName) {
		POI res;
		String coords = Settings.getValue("POI_" + poiName);
		if(coords.length() > 0) {
			String[] latLon = coords.split(";");
			float latitude = Float.parseFloat(latLon[0]);
			float longitude = Float.parseFloat(latLon[1]);
			res = new POI(poiName, latitude, longitude);

		} else {
			res = null;
		}
		return res;
	}

	POI store() {
		return store(this);
	}
	static POI store(POI poi) {
		String coords = poi.coordinates.getLatitude() + ";" + poi.coordinates.getLongitude();
		Settings.setValue("POI_" + poi.name, coords);
		return poi;
	}

	void setProximityIntent(PendingIntent proximityIntent) {
		this.proximityIntent = proximityIntent;
	}

	PendingIntent getProximityIntent() {
		return proximityIntent;
	}

	int getResourceDrawable() {
		return resourceDrawable;
	}

	boolean isDefined() {
		boolean res = coordinates.getLatitude() != 0 && coordinates.getLongitude() != 0;
		return res;
	}

	String getName() {
		return name;
	}

	Location getCoordinates() {
		return coordinates;
	}

	void setCoordinates(Location coordinates) {
		this.coordinates = new Location(coordinates);
	}

	public String toString() {
		String res = name + '@' + coordinates.getLatitude() + 'X' + coordinates.getLongitude();
		return res;
	}
}
